import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class GenController extends StatefulWidget {
  const GenController({super.key});

  @override
  GenControllerState createState() => GenControllerState();
}

class GenControllerState extends State<GenController> {
  final CounterController _counterCtrl = CounterController();
  final CounterControllerTwo _counterCtrlTwo = CounterControllerTwo();

  @override
  void dispose() {
    // TODO: implement dispose
    _counterCtrl.dispose();
    _counterCtrlTwo.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('构建 ctrl page');
    return Scaffold(
      appBar: AppBar(
        // 使用了 const 声明，build的时候不会重复构建
        title: const Text('自定义控制器 controller'),
      ),
      body: Center(
          child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // Text(
          //   '$counter',
          //   style: TextStyle(fontSize: 30),
          // ),
          Text(
            'foo1：',
          ),

          // foo 1
          Foo(counterCtrl: _counterCtrl),
          ElevatedButton(
              onPressed: () {
                _counterCtrl.value++;
              },
              child: const Text('增加')),
          Text(
            'foo2：',
          ),
          // foo2
          Foo2(counterCtrl: _counterCtrlTwo),
          ElevatedButton(
              onPressed: () {
                _counterCtrlTwo.counter.value++;
              },
              child: const Text('增加')),
          ElevatedButton(
              onPressed: () {
                _counterCtrlTwo.fontSize.value++;
              },
              child: const Text('增加字体'))
        ],
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _counterCtrl.value++;
        },
        child: const Icon(
          Icons.add,
          size: 30,
        ),
      ),
    );
  }
}

// 参数，复杂类型的 是指针传递，简单类型的是值传递
// 值传递： 复制一份传入子组件，子组件更新传入的状态，父组件并不会同步更改
// 把值包进一个class，变成复杂类型，就变成指针传递了
class Foo extends StatefulWidget {
  const Foo({super.key, required this.counterCtrl});

  final CounterController counterCtrl;

  @override
  _FooState createState() => _FooState();
}

class _FooState extends State<Foo> {
  @override
  Widget build(BuildContext context) {
    print('构建子组件');
    return Column(
      children: [
        AnimatedBuilder(
            animation: widget.counterCtrl,
            builder: (ctx, child) {
              print('构建builder 的listen组件');
              return Text(
                '${widget.counterCtrl.value}',
                style: const TextStyle(fontSize: 30),
              );
            })
      ],
    );
  }
}

class Foo2 extends StatefulWidget {
  const Foo2({super.key, required this.counterCtrl});

  final CounterControllerTwo counterCtrl;

  @override
  _Foo2State createState() => _Foo2State();
}

class _Foo2State extends State<Foo2> {
  @override
  Widget build(BuildContext context) {
    print('构建2子组件');
    return Column(
      children: [
        //ValueListenableBuilder 跟 animatedBuilder  ListenableBuilder 一样
        // ValueListenableBuilder(
        // 多个的情况
        AnimatedBuilder(
          animation: Listenable.merge([
            widget.counterCtrl.counter,
            widget.counterCtrl.fontSize,
          ]),
          builder: (ctx, child) {
            print('构建builder2 的listen组件');
            return Text(
              '${widget.counterCtrl.counter.value}',
              style: TextStyle(fontSize: widget.counterCtrl.fontSize.value),
            );
          },
        )
      ],
    );
  }
}

// 使用 自己写的ctrl， 只要把controller传入listenable,哪个值更新都会重新构建widget,因为每个set方法都调用了notifyListeners方法回调widget的rebuild
// 使用引用传递的方法，封装一个控制器，方便别的地方调用  原理类似 textfile的 controller
class CounterController extends ChangeNotifier {
  int _value = 0;

  int get value => _value;

  set value(int v) {
    _value = v;
    // 值更新后，通过 notifyListeners 调用，通知 builder方法重构build方法
    notifyListeners();
  }
}

// 使用 ValueNotifier 需要使用ctrl.title 传入listenable,多个的时候 使用Listenable.merge
// 使用 valueListenable 不用自定义get set
class CounterControllerTwo extends ChangeNotifier {
  ValueNotifier<int> counter = ValueNotifier(0);
  ValueNotifier<double> fontSize = ValueNotifier(20);
}
